/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.business.data.model;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import static com.google.common.base.Preconditions.checkNotNull;

public final class Language {
    private final StringProperty name = new SimpleStringProperty(this, "name");
    private final StringProperty fileName = new SimpleStringProperty(this, "fileName");

    private final BooleanProperty reference = new SimpleBooleanProperty();

    public boolean isReference() {
        return reference.get();
    }

    public void setReference(boolean reference) {
        this.reference.set(reference);
    }

    public Language(String name, String fileName) {
        setName(name);
        setFileName(fileName);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        checkNotNull(name);
        this.name.set(name);
    }

    public String getFileName() {
        return fileName.get();
    }

    public StringProperty fileNameProperty() {
        return fileName;
    }

    public void setFileName(String fileName) {
        checkNotNull(name);
        this.fileName.set(fileName);
    }

    @Override
    public String toString() {
        String referenceString = isReference() ? " - REFERENCE" : "";
        return getName() + " (" + getFileName() + ")" + referenceString;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Language language = (Language) o;

        return getFileName().equals(language.getFileName()) && getName().equals(language.getName());

    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + fileName.hashCode();
        return result;
    }
}
